package com.sxpi.zixun.service.questionscategory;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.zixun.bo.QuestionsCategoryBo;
import com.sxpi.zixun.common.enums.ResultStatusEnum;
import com.sxpi.zixun.common.ex.PugValidatorException;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.mapper.QuestionsCategoryMapper;
import com.sxpi.zixun.pojo.QuestionsCategory;
import com.sxpi.zixun.vo.QuestionsCategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 董伟豪
 * @Date: 2022/9/17 10:51
 */
@Service
@Slf4j
public class IQuestionsCategoryServiceImpl extends ServiceImpl<QuestionsCategoryMapper,QuestionsCategory> implements IQuestionsCategoryService {

    /**
     * 查询文章分类列表信息
     * @return
     */
    @Override
    public List<QuestionsCategoryBo> findQuestionsCategoryList() {
        // 1：设置查询条件
        LambdaQueryWrapper<QuestionsCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 2：查询发布的 0 未发布  1 发布
        lambdaQueryWrapper.eq(QuestionsCategory::getStatus,1);
        lambdaQueryWrapper.eq(QuestionsCategory::getIsdelete,0);
        lambdaQueryWrapper.orderByAsc(QuestionsCategory::getSorted);
        // 3: 查询返回
        return tranferListBo(this.list(lambdaQueryWrapper),QuestionsCategoryBo.class);
    }

    /**
     * 查询文章分类列表信息并分页
     * @param questionscategoryVo
     * @return
     */
    @Override
    public IPage<QuestionsCategoryBo> findQuestionsCategoryPage(QuestionsCategoryVo questionscategoryVo) {
        // 设置分页信息
        Page<QuestionsCategory> page = new Page<>(questionscategoryVo.getPageNo(),questionscategoryVo.getPageSize());
        // 设置查询条件
        LambdaQueryWrapper<QuestionsCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // lambdaQueryWrapper.select(QuestionsCategory.class, column -> !column.getColumn().equals("description"));
        // 根据关键词搜索信息
        lambdaQueryWrapper.like(Vsserts.isNotEmpty(questionscategoryVo.getKeyword()), QuestionsCategory::getTitle,questionscategoryVo.getKeyword());
        //查询发布的 0 未发布  1 发布
        lambdaQueryWrapper.eq(questionscategoryVo.getStatus() != null ,QuestionsCategory::getStatus,questionscategoryVo.getStatus());
        // 多列搜索
        // lambdaQueryWrapper.and(Vsserts.isNotEmpty(questionscategoryVo.getKeyword()),wrapper -> wrapper
        //         .like(QuestionsCategory::getName, questionscategoryVo.getKeyword())
        //         .or()
        //         .like(QuestionsCategory::getCategoryName, questionscategoryVo.getKeyword())
        // );
        // 根据时间排降序
        lambdaQueryWrapper.orderByDesc(QuestionsCategory::getCreateTime);
        // 查询分页返回
        IPage<QuestionsCategory> results = this.page(page,lambdaQueryWrapper);
        return tranferPageBo(results,QuestionsCategoryBo.class);
    }

    /**
     * 根据文章分类id查询明细信息
     * @param id
     * @return
     */
    @Override
    public QuestionsCategoryBo getQuestionsCategoryById(Long id) {
        QuestionsCategory questionsCategory = this.getById(id);
        return tranferBo(questionsCategory,QuestionsCategoryBo.class);
    }

    /**
     * 保存和修改文章分类
     * @param questionscategory
     * @return
     */
    @Override
    public QuestionsCategoryBo saveUpdateQuestionsCategory(QuestionsCategory questionscategory) {
        boolean flag = this.saveOrUpdate(questionscategory);
        return flag ? tranferBo(questionscategory,QuestionsCategoryBo.class)  : null;
    }

    /**
     * 根据文章分类id删除文章分类
     * @param id
     * @return
     */
    @Override
    public int deleteQuestionsCategoryById(Long id) {
        QuestionsCategory adminRole = this.getById(id);
        if (adminRole != null) {
            QuestionsCategory questionscategory2 = new QuestionsCategory();
            questionscategory2.setId(id);
            questionscategory2.setIsdelete(1);
            boolean b = this.updateById(questionscategory2);
            return 1;
        }
        return 0;
    }

    /**
     * 根据文章分类ids批量删除文章分类
     * @param batchIds
     * @return
     */
    @Override
    public boolean delBatchQuestionsCategory(String batchIds) {
        try {
            // 1 : 把数据分割
            String[] strings = batchIds.split(",");
            // 2: 组装成一个List<QuestionsCategory>
            List<QuestionsCategory> questionscategoryList = Arrays.stream(strings).map(idstr -> {
                QuestionsCategory questionscategory = new QuestionsCategory();
                questionscategory.setId(new Long(idstr));
                questionscategory.setIsdelete(1);
                return questionscategory;
            }).collect(Collectors.toList());
            // 3: 批量删除
            return this.updateBatchById(questionscategoryList);
        } catch (Exception ex) {
            throw new PugValidatorException(ResultStatusEnum.SERVER_DB_ERROR);
        }
    }
}
